<?php
class AclRulesController extends AppController {

	var $name = 'AclRules';
	var $uses=array('Resource','AclRule');
	function index($id=null) {
		
		$this->set('id',$id);
		$this->set('list',$this->Resource-> find('list', array('fields' => array('Resource.id','Resource.name'),'order'=>'Resource.id DESC')));
		//lay ra danh sach cac controler theo Id
		$this->set('ListAclRule',$this->AclRule->find('all',array('conditions'=>array('AclRule.role_id'=>$id))));
		
	}
	
	function add() {
		
		if(!empty($_POST['chkPrivileges']) && isset($_POST['chkPrivileges'])){
		foreach($_POST['chkPrivileges'] as $value){
			if($value==1){
				$this->data['Resource']['add']=$value;
			}elseif($value==2){
				$this->data['Resource']['eidt']=$value;
			}else{
				$this->data['Resource']['delete']=$value;
			}
		}
			$this->data['Resource']['role_id']=$this->data['Check']['id'];
			//pr($this->data);die();
			//kiem tran da truong controller roi thi khong cho add nua
			if($this->data['Resource']['resource_id'] && $this->data['Check']['id'] ){
				$check=$this->AclRule->find('all',array('conditions'=>array('AclRule.role_id'=>$this->data['Check']['id'],'AclRule.resource_id'=>$this->data['Resource']['resource_id'])));
				if(count($check)>0){
						echo "<script>alert('".json_encode('Đã tồn tại!')."');</script>";
						echo "<script>history.back(-1);</script>";
				}
				else {
					if($this->AclRule->save($this->data['Resource'])){
						$this->redirect('index'.'/'.$this->data['Check']['id']);
					}
				}
			}
			
		}
		else {
			echo "<script>alert('".json_encode('Ban chọn một quyền !')."');</script>";
			echo "<script>history.back(-1);</script>";
		}

	}
	//delete 
	
	function delete() {
			if($_POST['chkId']){
			$Idnews=$_POST['chkId'];
			foreach($Idnews as $id){
				$this->AclRule->delete($id);			
			}
			$this->Session->setFlash(__('Bạn Delete thành công', true));
			$name=$_SERVER['HTTP_REFERER'];
			$this->redirect($name);
		}

	} 
	
}
?>